<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\common\model\mdllogin;

/**
 * Description of LoginModel
 *
 * @author DevPHP
 * @version 2018-4-10
 */
class MemberLoginModel {
    
    /**
     * 
     * @param type $user_name
     * @param type $password
     * @param type $ip
     * @return type
     * @version 2018-4-10
     */
    public function loginWithPassword($user_name, $password, $ip) {
        $data = ['login_id' => $user_name, 'password' => $password];
        // 登录验证
        $loginValidate = new \app\common\validate\LoginWithPasswordValidate();
        if (!$loginValidate->check($data)) {
            return fmt_result(false, $loginValidate->getError());
        }
        return $this->loginHandle($user_name, ['user_name' => $user_name], 'id, user_name, password, avatar_url');
    }

    // 2018-4-10
    public function loginWithSmscode($mobile, $smscode, $ip) {
        $data = ['login_id' => $mobile, 'sms_code' => $smscode];
        // 登录验证
        $loginValidate = new \app\common\validate\LoginWithSmscodeValidate();
        if (!$loginValidate->check($data)) {
            return fmt_result(false, $loginValidate->getError());
        }
        return $this->loginHandle($mobile, ['mobile' => $mobile], 'id, user_name, avatar_url');
    }

    // 2018-4-10
    public function loginWithPasswordOrSmscode($login_id, $password, $smscode, $ip) {
        if ($smscode) {
            return $this->loginWithSmscode($login_id, $smscode, $ip);
        }
        return $loginModel->loginWithPassword($login_id, $password, $ip);
    }

    // 2018-4-10
    public function loginOrRegistWithSmscode($mobile, $smscode, $ip) {
        $data = ['login_id' => $mobile, 'sms_code' => $smscode];
        // 登录验证
        $loginValidate = new \app\common\validate\LoginWithSmscodeValidate();
        if (!$loginValidate->check($data)) {
            return fmt_result(false, $loginValidate->getError());
        }
        $result = $this->loginHandle($mobile, ['mobile' => $mobile], 'id, user_name, avatar_url');
        if (!$result['status'] && $result['code'] == 'USER_NOT_FOUND') {
            $data = [
                'mobile' => $mobile,
                'user_name' => '',
                'password' => '',
                'regist_ip' => $ip,
                'update_time' => date('Y-m-d H:i:s'),
            ];
            $affected_rows = db('user_admin')->insert($data);
            if ($affected_rows) {
                $result = $this->loginHandle($mobile, ['mobile' => $mobile], 'id, user_name, avatar_url');
            }
        }
        return $result;
    }

    // 2018-4-10
    private function loginHandle($login_id, $where, $field) {
        $loginErrorId = 'login_error_' . $login_id;
        $loginErrorTimeSpan = 5 * 60;
        $loginErrorCount = intval(\think\facade\Cache::store('temp_data')->get($loginErrorId));
        if ($loginErrorCount > $loginErrorTimeSpan) {
            return fmt_result(false, '登录错误超过5次,请5分钟后再次尝试!');
        }
        if (BlacklistModel::check($login_id)) {
            return fmt_result(false, '黑名单帐号禁止登录', 'FORBIDDEN_ACCOUNT');
        }
        if (BlacklistModel::check($ip)) {
            return fmt_result(false, '黑名单IP禁止登录', 'FORBIDDEN_IP');
        }
        $userData = db('user_admin')->where($where)->field($field)->find();
        if (!$userData) {
            LogModel::logLogin($login_id, $ip, '用户不存在', false);
            return fmt_result(false, '用户不存在!', 'USER_NOT_FOUND');
        }
        if (isset($userData['password']) && ($userData['password'] != $password)) {
            // 记录登录错误次数
            if ($loginErrorCount) {
                \think\facade\Cache::store('temp_data')->inc($loginErrorId);
            } else {
                \think\facade\Cache::store('temp_data')->set($loginErrorId, 1, $loginErrorTimeSpan);
            }
            LogModel::logLogin($login_id, $ip, '密码错误[' . $password . ']', false);
            return fmt_result(false, '密码错误!', 'INVALID_PASSWORD');
        }
        if ($userData['status'] != 1) {
            return fmt_result(false, '用户状态异常', 'INVALID_STATUS');
        }
        // 登录成功
        \think\facade\Cache::store('temp_data')->rm($loginErrorId);
        if (isset($userData['password'])) {
            unset($userData['password']);
        }
        LogModel::logLogin($login_id, $ip, '登陆成功', true);
        session('login_info', $userData);
        return fmt_result(true, $userData);
    }

    /**
     * 账号登出
     * @version 2018-4-10
     */
    public function logout() {
        $loginInfo = session('login_info');
        if ($loginInfo) {
            LogModel::logLogin($loginInfo['id'], $ip, '登出成功', true, 0);
        }
        session('login_info', null);
    }

}
